using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DotNetNuke.Data;
using DotNetNuke.Entities.Users;

namespace DNNDev.DNNModules.NewsBeast
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for NewsBeast
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 

    // [DNNtc.UpgradeEventMessage("01.01.01,04.00.02,04.01.00")]
    [DNNtc.BusinessControllerClass()]
    public class NewsBeastController // : ISearchable, IPortable
    {
        private int UserID
        {
            get { return UserController.Instance.GetCurrentUserInfo().UserID; }
        }

        #region "Public Methods"

        #region "Item"
        public ItemInfo InsertItem(ItemInfo item)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                item.CreatedOnDate = DateTime.Now;
                item.CreatedByUserID = UserID;
                item.LastModifiedByUserID = UserID;
                item.LastModifiedOnDate = DateTime.Now;
                repository.Insert(item);
                return item;
            }
        }

        public ItemInfo UpdateItem(ItemInfo item)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                item.LastModifiedByUserID = UserID;
                item.LastModifiedOnDate = DateTime.Now;
                repository.Update(item);
                return item;
            }
        }

        public void DeleteItem(ItemInfo item)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                repository.Delete(item);
            }
        }

        public IEnumerable<ItemInfo> GetAllItems(int moduleId)
        {
            IEnumerable<ItemInfo> items;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<ItemInfo>();
                items = repository.Find("WHERE ModuleId = @0", moduleId);
            }
            return items;
        }

        #endregion

        #region NL_Newsletter

        public IEnumerable<NewsletterInfo> GetNL_Newsletters()
        {
            IEnumerable<NewsletterInfo> nL_Newsletters;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<NewsletterInfo>();
                nL_Newsletters = repository.Get();
            }
            return nL_Newsletters;
        }

        public void DeleteNL_Newsletter(NewsletterInfo item)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<NewsletterInfo>();
                repository.Delete(item);
            }
        }

        public NewsletterInfo GetNL_Newsletter(int nL_NewsletterId)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<NewsletterInfo>();
                return repository.Find("WHERE ID = @0", nL_NewsletterId).FirstOrDefault();
            }
        }

        public int InsertNL_Newsletter(NewsletterInfo nL_Newsletter)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<NewsletterInfo>();
                nL_Newsletter.Date = DateTime.Now;
                repository.Insert(nL_Newsletter);
                return nL_Newsletter.ID;
            }
        }

        public void UpdateNL_Newsletter(NewsletterInfo nL_Newsletter)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<NewsletterInfo>();
                nL_Newsletter.Date = DateTime.Now;
                repository.Update(nL_Newsletter);
            }
        }

        #endregion

        #region NL_Newsletter_Text

        public IEnumerable<Newsletter_TextInfo> GetNL_Newsletter_Texts()
        {
            IEnumerable<Newsletter_TextInfo> nL_Newsletter_Texts;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<Newsletter_TextInfo>();
                nL_Newsletter_Texts = repository.Get();
            }
            return nL_Newsletter_Texts;
        }

        public Newsletter_TextInfo GetNL_Newsletter_Text(int nL_Newsletter_TextId)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<Newsletter_TextInfo>();
                return repository.Find("WHERE ID = @0", nL_Newsletter_TextId).FirstOrDefault();
            }
        }

        public int InsertNL_Newsletter_Text(Newsletter_TextInfo nL_Newsletter_Text)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<Newsletter_TextInfo>();
                repository.Insert(nL_Newsletter_Text);
                return nL_Newsletter_Text.ID;
            }
        }
        public void UpdateNL_Newsletter_Text(Newsletter_TextInfo nL_Newsletter_Text)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<Newsletter_TextInfo>();
                repository.Update(nL_Newsletter_Text);
            }
        }
        public void DeleteNL_Newsletter_Text(Newsletter_TextInfo nL_Newsletter_Text)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<Newsletter_TextInfo>();
                repository.Delete(nL_Newsletter_Text);
            }
        }

        #endregion

        #region NL_Settings

        public IEnumerable<SettingsInfo> GetNL_Settingss()
        {
            IEnumerable<SettingsInfo> nL_Settingss;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SettingsInfo>();
                nL_Settingss = repository.Get();
            }
            return nL_Settingss;
        }

        public SettingsInfo GetNL_Settings(int nL_SettingsId)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SettingsInfo>();
                return repository.Find("WHERE ID = @0", nL_SettingsId).FirstOrDefault();
            }
        }

        public int InsertNL_Settings(SettingsInfo nL_Settings)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SettingsInfo>();
                repository.Insert(nL_Settings);
                return nL_Settings.ID;
            }
        }
        public void UpdateNL_Settings(SettingsInfo nL_Settings)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SettingsInfo>();
                repository.Update(nL_Settings);
            }
        }
        public void DeleteNL_Settings(SettingsInfo nL_Settings)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SettingsInfo>();
                repository.Delete(nL_Settings);
            }
        }

        #endregion

        #region NL_Subscriber

        public IEnumerable<SubscriberInfo> GetNL_Subscribers()
        {
            IEnumerable<SubscriberInfo> nL_Subscribers;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SubscriberInfo>();
                nL_Subscribers = repository.Get();
            }
            return nL_Subscribers;
        }

        public SubscriberInfo GetNL_Subscriber(int nL_SubscriberId)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SubscriberInfo>();
                return repository.Find("WHERE ID = @0", nL_SubscriberId).FirstOrDefault();
            }
        }

        public int InsertNL_Subscriber(SubscriberInfo nL_Subscriber)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SubscriberInfo>();
                repository.Insert(nL_Subscriber);
                return nL_Subscriber.ID;
            }
        }
        public void UpdateNL_Subscriber(SubscriberInfo nL_Subscriber)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SubscriberInfo>();
                repository.Update(nL_Subscriber);
            }
        }
        public void DeleteNL_Subscriber(SubscriberInfo nL_Subscriber)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<SubscriberInfo>();
                repository.Delete(nL_Subscriber);
            }
        }

        #endregion

        #region NL_Subscriber_Newsletter

        public IEnumerable<Subscriber_NewsletterInfo> GetNL_Subscriber_Newsletters()
        {
            IEnumerable<Subscriber_NewsletterInfo> nL_Subscriber_Newsletters;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<Subscriber_NewsletterInfo>();
                nL_Subscriber_Newsletters = repository.Get();
            }
            return nL_Subscriber_Newsletters;
        }

        public void InsertNL_Subscriber_Newsletter(Subscriber_NewsletterInfo nL_Subscriber_Newsletter)
        {
            using (IDataContext context = DataContext.Instance())
            {
                string sqlCmd = "INSERT INTO {databaseOwner}[{objectQualifier}NL_Subscriber_Newsletter] (ID_Subscriber,ID_Newsletter) VALUES (@0,@1)";
                context.Execute(CommandType.Text, sqlCmd, nL_Subscriber_Newsletter.ID_Subscriber, nL_Subscriber_Newsletter.ID_Newsletter);
            }
        }
        public void UpdateNL_Subscriber_Newsletter(Subscriber_NewsletterInfo nL_Subscriber_Newsletter)
        {
            using (IDataContext context = DataContext.Instance())
            {
                string sqlCmd = "UPDATE {databaseOwner}[{objectQualifier}NL_Subscriber_Newsletter] SET ID_Subscriber = @0,ID_Newsletter = @1 WHERE  = @0";
                context.Execute(CommandType.Text, sqlCmd, nL_Subscriber_Newsletter.ID_Subscriber, nL_Subscriber_Newsletter.ID_Newsletter);
            }
        }

        public void DeleteNL_Subscriber_Newsletter(Subscriber_NewsletterInfo nL_Subscriber_Newsletter)
{
	using (IDataContext context = DataContext.Instance())
	{
		string sqlCmd = "DELETE FROM {databaseOwner}[{objectQualifier}NL_Subscriber_Newsletter] WHERE ID_Subscriber = @0 AND ID_Newsletter = @1";
		context.Execute(CommandType.Text, sqlCmd,nL_Subscriber_Newsletter.ID_Subscriber,nL_Subscriber_Newsletter.ID_Newsletter);
	}
}

        #endregion

        #region NL_Template

        public IEnumerable<TemplateInfo> GetNL_Templates()
        {
            IEnumerable<TemplateInfo> nL_Templates;
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<TemplateInfo>();
                nL_Templates = repository.Get();
            }
            return nL_Templates;
        }

        public TemplateInfo GetNL_Template(int nL_TemplateId)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<TemplateInfo>();
                return repository.Find("WHERE ID = @0", nL_TemplateId).FirstOrDefault();
            }
        }

        public int InsertNL_Template(TemplateInfo nL_Template)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<TemplateInfo>();
                repository.Insert(nL_Template);
                return nL_Template.ID;
            }
        }
        public void UpdateNL_Template(TemplateInfo nL_Template)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<TemplateInfo>();
                repository.Update(nL_Template);
            }
        }
        public void DeleteNL_Template(TemplateInfo nL_Template)
        {
            using (IDataContext context = DataContext.Instance())
            {
                var repository = context.GetRepository<TemplateInfo>();
                repository.Delete(nL_Template);
            }
        }

        #endregion

        #endregion


        //#region Implementation of ISearchable

        ///// ----------------------------------------------------------------------------- 
        ///// <summary> 
        ///// GetSearchItems implements the ISearchable Interface 
        ///// </summary> 
        ///// <remarks> 
        ///// </remarks> 
        ///// <param name="module">The ModuleInfo for the module to be Indexed</param> 
        ///// <history> 
        ///// </history> 
        ///// ----------------------------------------------------------------------------- 
        //public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo module)
        //{ 
        //    
        //    SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection(); 
        //    
        //    IEnumerable<ItemInfo> items = GetAllItems(module.ModuleID); 
        //    foreach ( ItemInfo item in items) 
        //    { 
        //        SearchItemInfo searchItem = new SearchItemInfo(module.ModuleTitle, 
        //				item.Title,
        //				item.CreatedByUserID ?? -1,
        //				item.CreatedOnDate, 
        //				module.ModuleID, 
        //				item.ItemId.ToString(), 
        //				item.Content, 
        //				"ItemId=" + item.ItemId.ToString()); 
        //        SearchItemCollection.Add(searchItem); 
        //    } 
        //    return SearchItemCollection; 
        //    
        //}

        //#endregion

        //#region Implementation of IPortable

        ///// ----------------------------------------------------------------------------- 
        ///// <summary> 
        ///// ExportModule implements the IPortable ExportModule Interface 
        ///// </summary> 
        ///// <remarks> 
        ///// </remarks> 
        ///// <param name="moduleID">The Id of the module to be exported</param> 
        ///// <history> 
        ///// </history> 
        ///// ----------------------------------------------------------------------------- 
        //public string ExportModule(int moduleID) 
        //{ 
        //    IEnumerable<ItemInfo> items = GetAllItems(moduleID); 
        //    string strXML = "<NewsBeast_Items>"; 
        //    foreach ( ItemInfo item in items) 
        //    { 
        //        strXML += "<NewsBeast_Item>"; 
        //        strXML += "<Title>" +XmlUtils.XMLEncode(item.Title) + "</Title>"; 
        //        strXML += "<Content>" +XmlUtils.XMLEncode(item.Content) + "</Content>";
        //        strXML += "<CreatedByUserID>" +XmlUtils.XMLEncode(item.CreatedByUserID.ToString()) + "</CreatedByUserID>";
        //        strXML += "<CreatedOnDate>" + XmlUtils.XMLEncode(item.CreatedOnDate.ToString()) + "</CreatedOnDate>";
        //        strXML += "</NewsBeast_Item>"; 
        //    } 
        //    strXML += "</NewsBeast_Items>"; 
        //    return strXML; 
        //}

        ///// ----------------------------------------------------------------------------- 
        ///// <summary> 
        ///// ImportModule implements the IPortable ImportModule Interface 
        ///// </summary> 
        ///// <remarks> 
        ///// </remarks> 
        ///// <param name="moduleID">The Id of the module to be imported</param> 
        ///// <param name="name">The name to be imported</param> 
        ///// <param name="version">The version of the module to be imported</param> 
        ///// <param name="userId">The Id of the user performing the import</param> 
        ///// <history> 
        ///// </history> 
        ///// ----------------------------------------------------------------------------- 
        //public void ImportModule(int moduleID, string name, string version, int userId) 
        //{ 
        //    
        //    XmlNode xmlItems = Globals.GetContent(name, "NewsBeast_Items"); 
        //    foreach ( XmlNode xmlItem in xmlItems.SelectNodes("NewsBeast_Item")) 
        //    { 
        //        ItemInfo item = new ItemInfo(); 
        //        item.ModuleId = moduleID; 
        //        item.Title = xmlItem.SelectSingleNode("Title").InnerText; 
        //        item.Company = xmlItem.SelectSingleNode("Company").InnerText;//        
        //        item.CreatedByUserID = Int32.Parse(xmlItem.SelectSingleNode("CreatedByUserID").InnerText);
        //        item.CreatedOnDate = DateTime.Parse(xmlItem.SelectSingleNode("CreatedOnDate").InnerText);
        //        InsertItem(item); 
        //    } 
        //}

        //#endregion

    }
}